/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.OutputConnection;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
import tst.TST;
import util.FileUtility;

/**
 * @author omega
 */
public class TestMidlet extends MIDlet implements CommandListener {

    Display d;
    TextBox tb = new TextBox("debug", "", 5000, TextField.ANY);
    Command cmdAction = new Command("action", Command.OK, 1);
    TST tst = new TST();

    public void startApp() {
        d = Display.getDisplay(this);
        d.setCurrent(tb);
        tb.addCommand(cmdAction);
        tb.setCommandListener(this);
        String[] key = {"nona", "hen ci", "atos", "namec", "defi" };
        String[] content = {"cewek", "banci", "keras", "hijau", "coklat"};
        tst.buildTree();
        System.out.println("START");
        for (int i = 0; i < key.length; i++) {
            tst.insert(key[i], content[i]);
            System.out.println(i+":"+key[i]);
        }
        System.out.println("END");
        for (int i = 0; i < key.length; i++) {
            String string = (String)tst.getValue(key[i]);
            System.out.println("value-"+i+":"+string);
        }

    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdAction) {
//            String a = FileUtility.convertInputstreamToString(getClass().getResourceAsStream("/tes.txt"));
//            System.out.println("a:" + a);
//            this.tb.setString((String)tst.getValue(tb.getString()));
//            String hasil = FileUtility.readFile("file://c:/myfile.txt;append=true");
            try {
//                System.out.println("create file");
//                Enumeration e = FileSystemRegistry.listRoots();
//                for (Enumeration obj = e; obj.hasMoreElements();) {
//                    String str = (String) e.nextElement();
//                    System.out.println("str:" + str);
//                }
//                FileConnection fileCon = (FileConnection) Connector.open("file:///root1/halo.txt", Connector.READ_WRITE);
//                if (!fileCon.exists()) {
//                    fileCon.create();
//                }
                String result =FileUtility.readFileCompressed("file:///root1/db_gi.dat");
                System.out.println("result:"+result);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
